package com.syh.jn.scm.service.impl.supplier.finance;

import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.supplier.entity.finance.PaymentBill;
import com.syh.jn.scm.domain.supplier.event.PaymentBillEvent;
import com.syh.jn.scm.domain.supplier.repository.finance.PaymentBillRepository;
import com.syh.jn.scm.dto.supplier.finance.PaymentBillMto;
import com.syh.jn.scm.dto.supplier.finance.PaymentBillSto;
import com.syh.jn.scm.dto.supplier.finance.PaymentBillVo;
import com.syh.jn.scm.service.api.supplier.finance.PaymentBillService;
import com.syh.jn.scm.service.api.supplier.finance.enums.PaymentBillEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 付款单接口实现
 */
@Service
@Transactional
public class PaymentBillServiceImpl extends AbstractServiceImpl<PaymentBillSto, PaymentBillMto, PaymentBillVo, PaymentBill, Long> implements PaymentBillService {

  @Autowired
  private PaymentBillRepository paymentBillRepositoryImpl;

  @Override
  protected AggregateRepository<PaymentBill, Long> getAggregateRepository() {
    return paymentBillRepositoryImpl;
  }

  /**
   * 保存付款单
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(PaymentBillSto sto) {
    PaymentBill bill = new PaymentBill(sto);
    Long id =  paymentBillRepositoryImpl.add(bill);
    this.publicEvent(PaymentBillEvent.paymentBillCreated(new Object[]{bill}));
    return id;
  }

  /**
   * 修改付款单
   *
   * @param mto
   */
  @Override
  public void update(PaymentBillMto mto) {
    PaymentBill bill = paymentBillRepositoryImpl.getById(mto.getId());
    bill.modify(mto);
    paymentBillRepositoryImpl.update(bill);
  }

  /**
   * 删除付款单
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    PaymentBill bill = paymentBillRepositoryImpl.getById(id);
    bill.destroy();
    paymentBillRepositoryImpl.remove(bill);
    this.publicEvent(PaymentBillEvent.paymentBillDestory(new Object[]{bill}));
  }

  /**
   * 确认付款
   *
   * @param id
   */
  @Override
  public void confirm(Long id) {
    PaymentBill bill = paymentBillRepositoryImpl.getById(id);
    bill.confirm();
    paymentBillRepositoryImpl.update(bill);

    //设置对账单的到账金额
    //发布事件消息
    if (bill.getLockStatus().equals(PaymentBillEnum.LOCK.getStatus())) {
      this.publicEvent(PaymentBillEvent.paymentBillLock(new Object[]{bill}));
    }
  }

  /**
   * 取消付款
   *
   * @param id
   */
  @Override
  public void cancel(Long id) {
    PaymentBill bill = paymentBillRepositoryImpl.getById(id);
    bill.cancel();
    paymentBillRepositoryImpl.update(bill);

    //设置对账单的到账金额还有状态
    //发布事件消息(未锁定并且已到账)
    if (bill.getLockStatus().equals(PaymentBillEnum.UNLOCK.getStatus()) && (
            //如果是供应商和客户 则判断对方是否为确认状态
            bill.getConfirmState().equals(PaymentBillEnum.OTHERPARTY_CONFIRM.getStatus())
            //如果是物流 则只判断是未锁状态 因为对方确认状态已同步取消
            || bill.getCustomerType() == 2)) {
      this.publicEvent(PaymentBillEvent.paymentBillUnlock(new Object[]{bill}));
    }
  }

  /**
   * 解锁
   *
   * @param id
   */
  @Override
  public void unLock(Long id) {
    PaymentBill bill = paymentBillRepositoryImpl.getById(id);
    bill.unLock();
    paymentBillRepositoryImpl.update(bill);
  }
}
